﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace PSAMMatrixCloud.Common.HttpParameterCommon
{
    public static class HttpParameterFilter
    {
        public const int T_RAW = 0x0;
        public const int T_STRING = 0x1;
        public const int T_INT = 0x2;
        public const int T_FLOAT = 0x4;
        public const int T_BOOL = 0x8;

        public const int T_EMAIL = 0x1000;
        public const int T_URL = 0x2000;
        public const int T_PHONE = 0x4000;
        public const int T_MOBILE = 0x8000;
        public const int T_LON = 0x8001;
        public const int T_LAT = 0x8002;
        public const int T_IDCARD = 0x8003;
        public const int T_BIT = 0x8004;//对应数据库的bit类型
        public const int T_GUID = 0x8005;//GUID


        public static bool filter(object t_value, int t_type, bool t_required = false)
        {
            if (t_required)
            {
                if (commonFunction.IsEmpty(t_value))
                {
                    return false;
                }
            }

            if (t_type == HttpParameterFilter.T_RAW)
            {
                return true;
            }
            if (!commonFunction.IsEmpty(t_value))
            {
                //int
                if (t_type == HttpParameterFilter.T_INT)
                {
                    return commonFunction.IsNumber(t_value.ToString());
                }

                //float
                else if (t_type == HttpParameterFilter.T_FLOAT)
                {
                    return commonFunction.IsDecimal(t_value.ToString());
                }

                //boolean
                else if (t_type == HttpParameterFilter.T_BOOL)
                {
                    if (t_value.ToString() != "0" && t_value.ToString() != "1")
                    {
                        return false;
                    }
                    return true;
                }

                //string above
                else if (t_type == T_STRING)
                {
                    return commonFunction.ProcessSqlStr(t_value.ToString());
                }

                else if (t_type == HttpParameterFilter.T_EMAIL)
                {
                    return commonFunction.IsEmail(t_value.ToString());
                }

                else if (t_type == HttpParameterFilter.T_PHONE)
                {
                    return commonFunction.IsPhoneNumber(t_value.ToString());
                }

                else if (t_type == HttpParameterFilter.T_MOBILE)
                {
                    return commonFunction.IsMobilePhoneNumber(t_value.ToString());
                }
                else if (t_type == HttpParameterFilter.T_LON)
                {
                    return commonFunction.IsLongitude(t_value.ToString());
                }
                else if (t_type == HttpParameterFilter.T_LAT)
                {
                    return commonFunction.IsLatitude(t_value.ToString());
                }
                else if (t_type == HttpParameterFilter.T_IDCARD)
                {
                    return commonFunction.IsIDCard(t_value.ToString());
                }
                else if (t_type == HttpParameterFilter.T_BIT)
                {
                    return (t_value.ToString() == "0" || t_value.ToString() == "1");
                }
                else if (t_type == T_GUID)
                {
                    Guid outGuid = Guid.NewGuid();
                    return Guid.TryParse(t_value.ToString(), out outGuid);
                }
            }
            return true;
        }

        /// <summary>
        /// 判断http参数且返回小写key的队列(排除掉空的值)
        /// </summary>
        /// <param name="dicIn">入参</param>
        /// <param name="dicFilter">检测规则</param>
        /// <returns></returns>
        public static Dictionary<string, object> CheckParameters(Dictionary<string, object> dicIn, List<HttpParamDefine> dicFilter)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            try
            {
                foreach (var item in dicFilter)
                {
                    HttpParamDefine filterModel = item;
                    object obj = null;
                    if (dicIn.Count(m => m.Key.ToLower() == item.pKey.ToLower()) > 0)
                    {
                        obj = dicIn[item.pKey.ToLower()];
                        if (!HttpParameterFilter.filter(obj, filterModel.pType, filterModel.pRequired))
                        {
                            throw new CustomException("参数" + item.pKey + "无效或不能为空");
                        }
                    }
                    else
                    {
                        if (filterModel.pRequired)
                        {
                            throw new CustomException("参数" + item.pKey + "是必须的");
                        }
                        continue;
                    }
                    result.Add(item.pKey.ToLower(), obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }
    }

    public class HttpParamDefine
    {
        public string pKey { get; set; }
        public int pType { get; set; }
        public bool pRequired { get; set; }
    }
}